package com.chinasofti.zbest.web;


import com.chinasofti.zbest.dao.model.Product;
import com.chinasofti.zbest.dao.model.ShopCart;
import com.chinasofti.zbest.dao.model.User;
import com.chinasofti.zbest.service.IProductService;
import com.chinasofti.zbest.service.IShopCartService;
import com.chinasofti.zbest.service.info.ProductInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;

import java.util.List;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author zbest
 * @since 2019-05-28
 */
@Controller
@Slf4j
public class ShopCartController extends BaseController {
    @Autowired
    private IShopCartService iShopCartService;
    @Autowired
    private IProductService iProductService;

    @RequestMapping("toProDetail")
    public String toProDetail(String productNumber,ModelMap modelMap,HttpSession session){
//        User user = (User)session.getAttribute("user");
//        log.info("用户名"+user.getUsername());
        Product product = iShopCartService.queryByProNum(productNumber);
//        modelMap.addAttribute("user",user);
        session.setAttribute("product",product);
        modelMap.addAttribute("product",product);
//        log.info(product.toString());

        log.info("猜你喜欢");
        List<ProductInfo> searchIde=iProductService.searchCategoryId(22);
        modelMap.addAttribute("searchIde",searchIde.subList(0,5));
        modelMap.addAttribute("searchIde1",searchIde.subList(4,9));
        log.info("{}",searchIde);

        log.info("猜你喜欢");
        List<ProductInfo> searchIdes=iProductService.searchCategoryId(4);
        modelMap.addAttribute("searchIdes",searchIdes.subList(0,5));
        log.info("{}",searchIdes);

        return "product/proDetail";
    }


    @RequestMapping("addToCart")
    public String addToCart(String categoryName,HttpSession session,ModelMap modelMap){
        User user = (User) session.getAttribute("user");
        log.info("购物车页面"+user.getUserId());
        Product product = (Product)session.getAttribute("product");
        log.info("购物车页面产品信息"+product);
        log.info(categoryName);
        //将商品加入购物车
        ShopCart shopCart = new ShopCart();
        shopCart.setProductNumber(product.getProductNumber());
        shopCart.setProductName(product.getProductName());
        shopCart.setCategoryName(categoryName);
        shopCart.setUserId(user.getUserId());
        shopCart.setPrice(product.getProductPrice());
        shopCart.setCountPrice(product.getProductPrice());
        shopCart.setProductPicture(product.getProductPicture());
        iShopCartService.addProduct(shopCart);
        return "redirect:tocart";
    }
    @RequestMapping("modifyToCartReduce")
    @ResponseBody
    public ShopCart modifyToCart(String productNumber,String count){
        log.info(productNumber);
        ShopCart shopCart = iShopCartService.queryForReduce(productNumber);
        log.info("数量前"+count);
        int countPro =Integer.parseInt(count)-1;
        String price1 = shopCart.getPrice().toString();
        double price = Double.parseDouble(price1);
        iShopCartService.reduceCountByProductNumber(productNumber,countPro,countPro*price);
        log.info("ServiceImpl"+"数量"+countPro+"总价"+(countPro*price));
        ShopCart result = iShopCartService.queryForReduce(productNumber);
        return result;
    }
    @RequestMapping("modifyToCartPlus")
    @ResponseBody
    public ShopCart modifyToCartPlus(String productNumber,String count){
        log.info(productNumber);
        ShopCart shopCart = iShopCartService.queryForReduce(productNumber);
        log.info("数量前"+count);
        int countPro =Integer.parseInt(count)+1;
        String price1 = shopCart.getPrice().toString();
        double price = Double.parseDouble(price1);
        iShopCartService.reduceCountByProductNumber(productNumber,countPro,countPro*price);
        log.info("ServiceImpl"+"数量"+countPro+"总价"+(countPro*price));
        ShopCart result = iShopCartService.queryForReduce(productNumber);
        return result;
    }
    @RequestMapping("deleteCart")
    public String delete(String productNumber,HttpSession session,ModelMap modelMap){
        User user = (User) session.getAttribute("user");
        log.info("购物车删除"+user.getUserId());

        log.info("购物车删除"+productNumber);
        iShopCartService.del(user.getUserId(),productNumber);

        //将商品从购物车删除

        return "redirect:tocart";
    }

    @RequestMapping("tocart")
    public String cart(HttpSession session,ModelMap modelMap){

        User user = (User) session.getAttribute("user");
        log.info("购物车中用户ID"+user.getUserId());
        //根据用户ID查购物车表，返回该用户购物车中商品
        List<ShopCart> productList = iShopCartService.query(user.getUserId());
        modelMap.addAttribute("productList",productList);
        return "cart/cart";
    }
}

